package com.web.option.controller;

import com.web.common.BaseResult;
import com.web.common.enums.BaseStatus;
import com.web.customer.entity.Customer;
import com.web.option.entity.Option;
import com.web.option.service.OptionService;
import com.web.qualification.entity.CustomerQualification;
import com.web.qualification.service.CustomerQualificationService;
import com.web.systemParam.entity.SystemParam;
import com.web.systemParam.service.SystemParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;
import java.util.logging.Logger;

/**
 * @author wenzhuo
 * @create 2024-07-07 2024/7/7
 */
@RestController
@RequestMapping("/option")
@CrossOrigin
public class OptionController {

    Logger logger = Logger.getLogger(this.getClass().getName());


    @Autowired
    private CustomerQualificationService qualificationService;

    @Autowired
    private SystemParamService systemParamService;

    @Autowired
    private OptionService optionService;


    @PostMapping(value = "/createOption")
    public BaseResult createOption(@ModelAttribute Option option, String userName){

        //判断交割日期是否处于参数范围内
        List<SystemParam> systemParamListMax = systemParamService.getSystemParamList("MAX_DAYS_OPTION_FIXING", "INT");
        if(systemParamListMax.isEmpty() || systemParamListMax.size() != 1){
            return new BaseResult(BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getCode(),BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getDesc());
        }
        String maxDays = systemParamListMax.get(0).getSystemParamValue();
        LocalDate maxDate = option.getTradeDate().plusDays(Long.valueOf(maxDays));

        List<SystemParam> systemParamListMin = systemParamService.getSystemParamList("MIN_DAYS_OPTION_FIXING", "INT");
        if(systemParamListMin.isEmpty() || systemParamListMin.size() != 1){
            return new BaseResult(BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getCode(),BaseStatus.SYSTEM_PARAM_DATA_EXCEPTION.getDesc());
        }
        String minDays = systemParamListMin.get(0).getSystemParamValue();
        LocalDate minDate = option.getTradeDate().plusDays(Long.valueOf(minDays));


       if(option.getExpireDate().isAfter(maxDate) || option.getExpireDate().isBefore(minDate)){
           return new BaseResult(BaseStatus.INVALID_EXPIRE_DATE.getCode(),BaseStatus.INVALID_EXPIRE_DATE.getDesc());
       }

        //校验客户是否有做期权交易的资质
        CustomerQualification qualificationByCusNo = qualificationService.getCustomerQualificationByCusNo(option.getCustomerNo());

        if(qualificationByCusNo == null){
            return new BaseResult(BaseStatus.INVALID_OPTION_QUALIFICATION.getCode(),BaseStatus.INVALID_OPTION_QUALIFICATION.getDesc());
        }

        if(!"Y".equals(qualificationByCusNo.getAllowedOption())){
            return new BaseResult(BaseStatus.INVALID_OPTION_ACCESS.getCode(),BaseStatus.INVALID_OPTION_ACCESS.getDesc());
        }

        option.setMaker(userName);
        //新增Option时处理
        return optionService.handleOption(option);

    };

    @RequestMapping(value = "/optionList")
    public BaseResult<List<Option>> getOptionList(@ModelAttribute Option option){

        List<Option> optionList = optionService.getOptionList(option);

        if(optionList.isEmpty()){
            return new BaseResult(BaseStatus.OPTION__DATA_NOT_EXIST.getCode(),BaseStatus.OPTION__DATA_NOT_EXIST.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),optionList);
    }


    @PostMapping(value = "/reject")
    public BaseResult<String> rejectOption(Integer optionNumber, String userName, String remark){

        Integer rejectResult = optionService.rejectOption(optionNumber, userName,remark);

        if(rejectResult <= 0){
            return new BaseResult(BaseStatus.OPTION_REJECT_FAILED.getCode(),BaseStatus.OPTION_REJECT_FAILED.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.OPTION_REJECT_SUCCESS.getDesc());
    }

    @PostMapping(value = "/confirm")
    public BaseResult<String> confirmOption(Integer optionNumber, String userName, String remark){
        //根据OptionNumber查询出Option交易
        Option option = optionService.findOptionByOptionNo(optionNumber);


        //判断maker和checker是否为同一人
        if(userName.equals(option.getMaker())){
            return new BaseResult(BaseStatus.MAKER_APPROVER_THE_SAME.getCode(),BaseStatus.MAKER_APPROVER_THE_SAME.getDesc());
        }

        //扣账/入账
        option.setChecker(userName);
        option.setRemark(remark);
        return optionService.confirmOption(option);
    }

    @RequestMapping(value = "/settled")
    public BaseResult<String> settlementOption(){

        //查询出status=NOR的Option交易
        List<Option> optionsNor = optionService.getOptionListByStatusNor(LocalDate.now());
        if(optionsNor.isEmpty()){
            return new BaseResult(BaseStatus.OPTION__DATA_NOT_EXIST.getCode(),BaseStatus.OPTION__DATA_NOT_EXIST.getDesc());
        }

        if(optionService.settlementOption(optionsNor) < 0){
            return new BaseResult(BaseStatus.OPTION_SETTLED_FAILED.getCode(),BaseStatus.OPTION_SETTLED_FAILED.getDesc());
        }

        return new BaseResult(BaseStatus.SUCCESS.getCode(),BaseStatus.OPTION_SETTLED_SUCCESS.getDesc());
    }

}
